package me.firstapp.bbs.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import me.firstapp.bbs.base.BaseController;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.bbs.JsonMember;
import me.firstapp.common.module.bbs.JsonPointAccount;
import me.firstapp.common.module.shop.JsonAddress;
import me.firstapp.common.module.shop.JsonOrder;
import me.firstapp.common.module.shop.JsonProduct;
import me.firstapp.common.module.shop.JsonProductComment;
import me.firstapp.common.module.shop.JsonShoppingCart;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.spi.bbs.MemberService;
import me.firstapp.spi.shop.AddressService;
import me.firstapp.spi.shop.OrderService;
import me.firstapp.spi.shop.PointService;
import me.firstapp.spi.shop.ProductCommentService;
import me.firstapp.spi.shop.ProductService;
import me.firstapp.spi.shop.ShoppingCartService;

@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {

	@Autowired
	private AddressService addressService;

	@Autowired
	private OrderService orderService;

	@Autowired
	private ShoppingCartService shoppingCartService;

	@Autowired
	private MemberService memberService;

	@Autowired
	private PointService pointService;

	@Autowired
	private ProductService productService;

	@Autowired
	private ProductCommentService productCommentService;

	@RequestMapping(value = "/orderAccount")
	public ModelAndView orderAccountView(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Long memberId = getCurrentLoginMemberId(request, response);
		SingleObject<JsonAddress> result = addressService.findAddressByMemberId(memberId);
		modelMap.put("address", result.getObject());
		SingleObject<JsonShoppingCart> _result = shoppingCartService.findMemberShoppingCart(memberId);
		modelMap.put("shoppingCart", _result.getObject());
		return new ModelAndView("shop/orderAccount", modelMap);
	}

	@RequestMapping(value = "/orderCommit")
	public ModelAndView orderCommitView(String remark, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		Long memberId = getCurrentLoginMemberId(request, response);
		SingleObject<JsonOrder> result = orderService.createOrder(memberId, remark);
		modelMap.put("order", result.getObject());
		SingleObject<JsonAddress> _result = addressService.findAddressByMemberId(memberId);
		modelMap.put("address", _result.getObject());
		// 清除session中的购物车信息
		request.getSession().removeAttribute("shoppingCart");
		return new ModelAndView("shop/orderCommit", modelMap);
	}

	@RequestMapping(value = "/buyNow")
	public ModelAndView buyNowView(Long productId, Long productSkuRelId, HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) {
		Long memberId = getCurrentLoginMemberId(request, response);
		SingleObject<JsonMember> result = memberService.findMemberById(memberId);
		JsonMember member = result.getObject();
		JSONArray jsonArray = new JSONArray();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("productId", productId);
		jsonObject.put("productSkuRelId", productSkuRelId);
		jsonObject.put("counts", 1);
		jsonArray.add(jsonObject);
		shoppingCartService.updateShoppingCart(member.getShoppingCart().getId(), JSONArray.toJSONString(jsonArray));

		return new ModelAndView("redirect:orderAccount", modelMap);
	}

	@RequestMapping(value = "/orderPay")
	public ModelAndView orderPayView(String orderNo, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		Long memberId = getCurrentLoginMemberId(request, response);
		SingleObject<JsonPointAccount> result = pointService.findPointAccount(memberId);
		modelMap.put("pointAccount", result.getObject());
		SingleObject<JsonOrder> _result = orderService.findOrderByOrderNo(orderNo, false);
		modelMap.put("order", _result.getObject());
		return new ModelAndView("shop/orderPay", modelMap);
	}

	@RequestMapping(value = "/orderPay.do", method = RequestMethod.POST)
	public void orderPay(String orderNo, String password, HttpServletRequest request, HttpServletResponse response) {
		Long memberId = getCurrentLoginMemberId(request, response);
		SingleObject<JsonOrder> result = orderService.payOrder(memberId, orderNo, password);
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/orderPaySuccess")
	public ModelAndView orderPaySuccessView(String orderNo, HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		Long memberId = getCurrentLoginMemberId(request, response);
		SingleObject<JsonOrder> result = orderService.findOrderByOrderNo(orderNo, true);
		modelMap.put("order", result.getObject());
		SingleObject<JsonAddress> _result = addressService.findAddressByMemberId(memberId);
		modelMap.put("address", _result.getObject());
		return new ModelAndView("shop/orderPaySuccess", modelMap);
	}

	@RequestMapping(value = "/cancelOrder.do", method = RequestMethod.POST)
	public void cancelOrder(String orderNo, HttpServletRequest request, HttpServletResponse response) {
		getCurrentLoginMemberId(request, response);
		SingleObject<JsonOrder> result = orderService.cancelOrder(orderNo);
		ResponseUtils.renderJson(response, result);
	}
	
	@RequestMapping(value = "/productComment")
	public ModelAndView productCommentView(String orderNo, Long productId, HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) {
		SingleObject<JsonProduct> result = productService.findProductById(productId);
		modelMap.put("product", result.getObject());
		modelMap.put("orderNo", orderNo);
		modelMap.put("productId", productId);
		return new ModelAndView("shop/productComment", modelMap);
	}

	@RequestMapping(value = "/addProductComment.do", method = RequestMethod.POST)
	public void addProductComment(String content, String orderNo, Long productId, HttpServletRequest request,
			HttpServletResponse response) {
		Long memberId = super.getCurrentLoginMemberId(request, response);
		SingleObject<JsonProductComment> result = productCommentService.addProductComment(content, orderNo, productId,
				memberId);
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/distributeCall.do", method = RequestMethod.POST)
	public void distributeCall(String orderNo, HttpServletRequest request, HttpServletResponse response) {
		Long memberId = super.getCurrentLoginMemberId(request, response);
		SingleObject<Object> result = orderService.addUpdateDistributeCall(memberId, orderNo);
		ResponseUtils.renderJson(response, result);
	}
}
